package com.wffz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wffz.comm.utils.CommUtils;
import com.wffz.mapper.ClientUserMapper;
import com.wffz.po.ClientUser;
import com.wffz.po.MoneyLog;
import com.wffz.service.MoneyLogService;
import com.wffz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sunlei
 * @className UserServiceImpl
 * @Description
 * @date 2022-12-23 17:12
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private ClientUserMapper userMapper;

    @Override
    public ClientUser findUserByUserName(String loginName) {
        ClientUser clientUser = null;
        QueryWrapper<ClientUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name" , loginName);
        List<ClientUser> list = userMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            clientUser = list.get(0);
        }
        return clientUser;
    }

    @Override
    public ClientUser findUserByUserId(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public boolean saveClientUser(ClientUser clientUser) {
        return userMapper.insert(clientUser) > 0;
    }

    @Override
    public boolean isExistClientUser(String loginName) {
        ClientUser clientUser = null;
        QueryWrapper<ClientUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name" , loginName);
        List<ClientUser> list = userMapper.selectList(wrapper);
        return CommUtils.isNotEmptyList(list);
    }

    @Override
    public boolean updateClientUser(ClientUser clientUser) {
        int result = userMapper.updateById(clientUser);
        return result >= 1;
    }

    @Override
    public ClientUser findUserByinvitationCode(String rcode) {
        ClientUser clientUser = null;
        QueryWrapper<ClientUser> wrapper = new QueryWrapper<>();
        wrapper.eq("invitation_code" , rcode);
        List<ClientUser> list = userMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            clientUser = list.get(0);
        }
        return clientUser;
    }

    @Override
    public boolean validateUser(String loginName, String password) {
        boolean flag = false;
        QueryWrapper<ClientUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name" , loginName);
        wrapper.eq("login_password" , password);
        List<ClientUser> list = userMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

    @Resource
    private MoneyLogService moneyLogService;

    @Override
    public float getWithDrawAmount(ClientUser clientUser) {
        int level = clientUser.getVipLevel();
        float balance = clientUser.getBalance();
        // 获取充值金额
        float sumMoney = moneyLogService.sumMoney(clientUser);
        float txje = 10000000000f;
        switch (level) {
            case 0:
                txje = 0;
                break;
            case 1:
                txje = sumMoney * 1.25f;
                break;
            case 2:
                txje = sumMoney * 1.40f;
                break;
            case 3:
                txje = sumMoney * 1.45f;
                break;
            case 4:
                txje = sumMoney * 1.50f;
                break;
            case 5:
                txje = sumMoney * 1.60f;
                break;
            case 6:
                txje = sumMoney * 1.80f;
                break;
            case 7:
                txje = sumMoney * 2.00f;
                break;
            case 8:
                break;
        }
        if (balance > txje) {
            return txje;
        } else {
            return balance;
        }
    }

    @Override
    public int shareCount(ClientUser clientUser) {
        QueryWrapper<ClientUser> queryWrapper = new QueryWrapper<ClientUser>();
        queryWrapper.eq("rcode" , clientUser.getInvitationCode());
        return userMapper.selectCount(queryWrapper);
    }
}
